package com.iwown.sample4GApi.service;

import com.alibaba.fastjson2.JSON;
import com.google.protobuf.InvalidProtocolBufferException;
import com.iwown.sample4GApi.bean.AppConstant;
import com.iwown.sample4GApi.bean.EcgPacket;
import com.iwown.sample4GApi.protobuf.Om0Command;
import com.iwown.sample4GApi.utils.MyDateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OldManDataParser implements IDataParser {
    private static final Logger log = LoggerFactory.getLogger(OldManDataParser.class);

    private final String deviceId;
    private final RedisTemplate redisTemplate;
    public OldManDataParser(String deviceId, RedisTemplate redisTemplate) {
        this.deviceId = deviceId;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void DataParse(byte[] payload) {
        Om0Command.OM0Report report = null;
        try {
            report = Om0Command.OM0Report.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }

        //data time
        int seconds = report.getDateTime().getDateTime().getSeconds();
        String data_time = MyDateTimeUtils.convert2DateTimeString(seconds);

        //realtime step/distance/calorie
        float distance = report.getHealth().getDistance()*0.1F;
        float calorie = report.getHealth().getCalorie()*0.1F;
        log.info(String.format("%s steps:%d, distance:%f, calorie:%f",data_time,report.getHealth().getSteps(),distance,calorie));

        //device batterylevel/rssi
        log.info(String.format("%s battery:%d, rssi:%d",data_time,report.getBattery().getLevel(),report.getRssi()));

        //gnss location
        //*notice: the location upload by device is in WGS_84 coordinate system, not GCJ_02
        List<Om0Command.OM0TrackData> gnssList = report.getTrackDataList();
        List<EcgPacket.GnssList> gnssLists = new ArrayList<>(gnssList.size());
        if(gnssList != null){
            for(int i=0;i<gnssList.size();i++){
                Om0Command.OM0TrackData gnss = gnssList.get(i);
                String gnss_date = MyDateTimeUtils.convert2DateTimeString(gnss.getTime().getDateTime().getSeconds());
                log.info(String.format("time:%s, lon:%f, lat:%f, loc_type:%d",
                        gnss_date,gnss.getGnss().getLongitude(),
                        gnss.getGnss().getLatitude(),
                        gnss.getGpsType().ordinal()));

                EcgPacket.GnssList gnssItem = new EcgPacket.GnssList();
                gnssItem.setLon(gnss.getGnss().getLongitude());
                gnssItem.setLat(gnss.getGnss().getLatitude());
                gnssItem.setLoc_type(gnss.getGpsType().ordinal());
                gnssItem.setTime(gnss_date);
                gnssLists.add(gnssItem);
            }
        }


        EcgPacket ecgPacket = new EcgPacket();
        ecgPacket.setImei(deviceId);
        ecgPacket.setBattery(report.getBattery().getLevel());
        ecgPacket.setRssi(report.getRssi());
        ecgPacket.setSteps(report.getHealth().getSteps());
        ecgPacket.setDistance(distance);
        ecgPacket.setCalorie(calorie);
        ecgPacket.setTime(data_time);
        ecgPacket.setGnssList(gnssLists);
        log.info("ecgPacket:{}", JSON.toJSONString(ecgPacket));
        redisTemplate.opsForHash().putAll(AppConstant.ECG_PACKET + deviceId, JSON.toJavaObject(JSON.toJSON(ecgPacket), Map.class));
        redisTemplate.expire(AppConstant.ECG_PACKET + deviceId, 10,  TimeUnit.MINUTES);
    }
}
